<?php
error_reporting(E_ALL);

echo "Setting Time out limit to zero<br />";

/* Allow the script to hang around waiting for connections. */
set_time_limit(0);

/* Turn on implicit output flushing so we see what we're getting
 * as it comes in. */
ob_implicit_flush();

//$_SERVER['SERVER_ADDR'];//undefined from the cmd line
$address = '127.0.0.1';
$port = 5568;

//http://www.php.net/manual/en/sockets.constants.php
//socket_create ( int $domain , int $type , int $protocol )
//AF_INET is IPv4 Internet based protocols.
//SOCK_DGRAM is connectionless, unreliable messages of a fixed maximum length). 
//SOL_UDP represents the UDP protocol is based on this socket type.

/*Attempt 1
if (($sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) === false) {
	echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}

$from = '';
$notNeeded = 0;

if (socket_recvfrom($sock, $buf, 2, 0, $from, $notNeeded) === false) {
    echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
*/

/*attempt 2
$socket = stream_socket_server("udp://$address:$port", $errno, $errstr, STREAM_SERVER_BIND);
if (!$socket) {
    die("$errstr ($errno)");
}

echo "Socket up and Listening\n";

do {
    $pkt = stream_socket_recvfrom($socket, 2, 0, $peer);
    echo "$peerasdf\n";
    //stream_socket_sendto($socket, date("D M j H:i:s Y\r\n"), 0, $peer);
} while ($pkt !== false);
*/


//attempt 3
$address = '127.0.0.1';
$recport = 5568;

$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind($socket, $address, $recport);

$from = '';
$port = 0;
socket_recvfrom($socket, $buf, 2, 0, $from, $port);
socket_close ($socket);
echo "Received $buf" . PHP_EOL;

/* Send the Mode Change */
/* The Modes are:
* 0 - Standby
* 1 - Test
* 2 - Battle (Autonomous)
* 3 - Remote
*/

switch($buf){
	case 0:
		$newModeString = "System in Standby Mode";
		$id = 'StandbyModeButton';
		$color = '00FF00';
		echoScriptToChangeMode($id,$newModeString,$color );
		break;
	case 1:
		$newModeString = "System in Test Mode";
		$id = 'TestModeButton';
		$color = '0000FF';
		echoScriptToChangeMode($id,$newModeString,$color );
		break;
	case 2:
		$newModeString = "System in Battle Mode";
		$id = 'BattleModeButton';
		$color = 'FF0000';
		echoScriptToChangeMode($id,$newModeString,$color );
		break;
	case 3:
		$newModeString = "System in Remote Mode";
		$id = 'RemoteModeButton';
		$color = 'FFFF00';
		echoScriptToChangeMode($id,$newModeString,$color );
		break;
	default:
		echo "<script>parent.ReloadModeChangeListener();</script>";
}
	
function echoScriptToChangeMode($id,$newModeString,$color)
{
	echo "
	<script>
	try{
		alert('$newModeString');	
		parent.document.getElementById('$id').checked=true;
		parent.document.body.style.backgroundColor='$color';
		parent.ReloadModeChangeListener();
	}catch(err){
		alert('Error in Change Mode listener: ' + err.toString());
	}
	</script>
	
	
	";

}
?>